import Vue from 'vue'
//路由
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//注册vuex
import Vuex from 'vuex'
Vue.use(Vuex)
//进入立即获取本地存储中的car数据
var car = JSON.parse(localStorage.getItem('car') || '[]')
var store = new Vuex.Store({
    state: {
        car: car //存储购物车的数据{id:商品id，count：购买数量，price：商品单价，select：false}
    },
    mutations: {
        addToCar(state, goodsinfo) { //点击加入购物车 将商品信息保存到store的car中
            var flag = false
            state.car.some(item => {
                if (item.id == goodsinfo.id) {
                    item.count += parseInt(goodsinfo.count)
                    flag = true
                    return true
                }
            })
            if (!flag) {
                state.car.push(goodsinfo)
            }
            //更新car后存到本地localstorage
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updateGoodsInfo(state, goodsinfo) {
            //修改购物车数量值
            state.car.some(item => {
                if (item.id == goodsinfo.id) {
                    item.count = parseInt(goodsinfo.count)
                    return true
                }
            })
            //修改完商品的数量 把新的购物车数据保存到本地
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        removeFromCar(state, id) {
            //根据id从store中的购物车删除对于商品
            state.car.some((item, i) => {
                if (item.id == id) {
                    state.car.splice(i, 1)
                    return true
                }
            })
            //删除完完商品 把新的购物车数据保存到本地
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updateGoodsSelected(state, info) {
            state.car.some(item => {
                if (item.id == info.id) {
                    item.selected = info.selected
                }
            })
            //修改完selected后  把新的购物车数据保存到本地
            localStorage.setItem('car', JSON.stringify(state.car))
        }
    },
    getters: {
        getAllCount(state) {
            var c = 0;
            state.car.forEach(item => {
                c += item.count
            })
            return c
        },
        getGoodsCount(state) {
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.count
            })
            return o
        },
        getGoodsSelected(state) {
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.selected
            })
            return o
        },
        getGoodsCountAndNmount(state) {
            var o = {
                count: 0,
                amount: 0
            }
            state.car.forEach(item => {
                if (item.selected) {
                    o.count += item.count
                    o.amount += item.price * item.count
                }
                
            })
            return o
        }
    }
})
//定义时间全局过滤器
Vue.filter('dateFormat', function (dateStr, pattern = '') {
    var dt = new Date(dateStr)
    var y = dt.getFullYear()
    var m = (dt.getMonth() + 1).toString().padStart(2, '0')
    var d = dt.getDate().toString().padStart(2, '0')


    if (pattern.toLowerCase() === 'yyyy-mm-dd') {
        return `${y}-${m}-${d}`
    } else {
        var hh = dt.getHours().toString().padStart(2, '0')
        var mm = dt.getMinutes().toString().padStart(2, '0')
        var ss = dt.getSeconds().toString().padStart(2, '0')
        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
    }
})
//导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)
// 设置请求路径
Vue.http.options.root = 'http://localhost:3000/src/lib/data'


//导入Mui样式
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
//导入Mint-ui组件
// import {Header,Swipe,SwipeItem,Button} from 'mint-ui'
// Vue.component(Header.name,Header)
// Vue.component(Swipe.name,Swipe)
// Vue.component(SwipeItem.name,SwipeItem)
// Vue.component(Button.name,Button)

// import { Lazyload } from 'mint-ui';
// Vue.use(Lazyload);
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

//导入router.js路由模块
import router from './router.js'

//app跟组件
import app from './App.vue'
var vm = new Vue({
    el: '#app',
    render: c => c(app),
    router,
    store //挂载store
})